package cn.itsy.web.controller;

import cn.itsy.dao.EmpsDao;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("akun")
public class EmpsController {
    @Resource
    private EmpsDao ed;
    @Resource
    private Pagedao pd;

    //查询
    @RequestMapping("list")
    public ModelAndView show(ModelAndView ms, String empname, String sex, String deptid, String ductid, String jobid, String id, String state, String currentPage) {
        String sql = "select e.id,e.empid,e.empname,d.dname,t.zname,e.sex,e.idcard,e.birthday,e.education,j.name,e.officephone from emp e,empstatus s,dept d,job j,duct t where e.is_status=s.id and e.depart_id=d.id and e.job_id=j.id and  e.duct_id=t.id and e.is_status = '1'";
        String sql2 = "select * from dept where 1=1";
        String sql3 = "select * from duct where 1=1";
        String sql4 = "select * from job where 1=1";
        Page page = new Page();
        if (empname != null && !empname.equals("")) {
            sql += " and e.empname like '%" + empname + "%' ";
        }
        if (sex != null && !sex.equals("")) {
            sql += " and e.sex like '%" + sex + "%' ";
        }
        if (deptid != null && !deptid.equals("")) {
            sql += " and e.depart_id = '" + deptid + "' ";
        }
        if (ductid != null && !ductid.equals("") && !ductid.equals("0")) {
            sql += " and e.duct_id = '" + ductid + "' ";
        }
        if (jobid != null && !jobid.equals("")) {
            sql += " and e.job_id = '" + jobid + "' ";
        }
        if (state != null && !state.equals("asc")) {
            sql += " order by e.id asc ";
        }
        if (state != null && !state.equals("desc")) {
            sql += " order by e.id desc ";
        }
        Page empshowfy = pd.empshowfy(sql, currentPage, page);
        List<Dept> dl = ed.queryAdd(sql2);
        List<Duct> tl = ed.queryAdd1(sql3);
        List<Job> jl = ed.queryAdd2(sql4);
        ms.addObject("dl", dl);
        ms.addObject("tl", tl);
        ms.addObject("jl", jl);
        ms.addObject("empshowfy", empshowfy);
        ms.setViewName("/empshow");
        return ms;
    }

    //删除
    @RequestMapping("del")
    public String del(HttpServletRequest request, HttpServletResponse response, String eid) {
        String sql = "delete from emp where 1=1 and id = '" + eid + "'";
        ed.updeteid(sql);
        //System.out.println("1111");
        return "redirect:list";
    }

    //修改(跳转页面)
    @RequestMapping("update")
    public ModelAndView toupdate(ModelAndView mt, String id) {
        String sql = "select * from emp where emp.id = '" + id + "'";
        List<Emp> ul = ed.queryAll(sql);
        mt.addObject("ul", ul);
        String sql1 = "select * from dept where 1=1";
        List<Dept> dl = ed.queryAdd(sql1);
        mt.addObject("dl", dl);
        String sql2 = "select * from duct where 1=1";
        List<Duct> tl = ed.queryAdd1(sql2);
        mt.addObject("tl", tl);
        String sql3 = "select * from job where 1=1";
        List<Job> jl = ed.queryAdd2(sql3);
        mt.addObject("jl", jl);
        String sql4 = "select * from empstatus where 1=1";
        List<Job> sl = ed.queryAdd2(sql4);
        mt.addObject("sl", sl);
        mt.setViewName("/upadd");
        return mt;
    }

    //修改
    @RequestMapping("upadd")
    public String update(Emp emp) {
        System.out.println("进来了");
        String sql = "update emp set empid='" + emp.getEmpid() + "',empname='" + emp.getEmpname() + "',depart_id='" + emp.getDepartId() + "',sex='" + emp.getSex() + "',idcard='" + emp.getIdcard() + "',birthday='" + emp.getBirthday() + "',education='" + emp.getEducation() + "',job_id='" + emp.getJobId() + "',officephone='" + emp.getOfficephone() + "' where id='" + emp.getId() + "'";
        ed.updateById(sql);
        return "redirect:list";
    }

    //添加(跳转页面)
    @RequestMapping("toadd")
    public ModelAndView toadd(ModelAndView mt) {
        String sql = "select * from emp where 1=1";
        List<Emp> el = ed.queryAll(sql);
        mt.addObject("el", el);
        String sql1 = "select * from dept where 1=1";
        List<Dept> dl = ed.queryAdd(sql1);
        mt.addObject("dl", dl);
        String sql2 = "select * from job where 1=1";
        List<Job> jl = ed.queryAdd2(sql2);
        mt.addObject("jl", jl);
        String sql3 = "select * from empstatus where 1=1";
        List<Empstatus> sl = ed.queryAdd3(sql3);
        mt.addObject("sl", sl);
        String sql4 = "select * from duct where 1=1";
        List<Duct> tl = ed.queryAdd1(sql4);
        mt.addObject("tl", tl);

        mt.setViewName("/addemp");
        return mt;
    }

    //添加
    @RequestMapping("addemp")
    public String addemp(HttpServletRequest request, HttpServletResponse response, Emp e) {
        String sqls = "select * from emp where empid ='" + e.getEmpid() + "'";
        List<Emp> empList = ed.queryAll(sqls);
        if (empList.isEmpty()) {
            String sql = "insert into emp values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ed.add(sql, e);
            request.setAttribute("check", "success");
            return "redirect:list";
        } else {
            request.setAttribute("check", "error");
        }
        return "/addemp";
    }

    /**
     * 最基本的查需员工
     */
    @RequestMapping(value = "qea")
    @ResponseBody
    public List<Emp> queryEmpAll() {
        String sql = "select * from emp where emp.is_status=1";
        List<Emp> emps = this.ed.queryEmpAll(sql);
        return emps;
    }
}